package com.wd.jetpack.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import androidx.core.app.ActivityCompat
import com.wd.jetpack.R
import com.wd.jetpack.annotation.SingleClick
import com.wd.jetpack.base.BaseActivity
import com.wd.jetpack.databinding.ActivitySplashBinding
import com.wd.jetpack.ui.main.DataBindingActivity
import com.wd.jetpack.ui.main.DemoRoomActivity
import com.wd.jetpack.ui.main.ListActivity
import com.wd.jetpack.ui.main.RemoteActivity
import com.wd.jetpack.ui.viewmodel.SplashViewModel

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V5.9.3, 2021/6/4]
 * @since V5.9.3
 */
class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    private val mViewModel: SplashViewModel by lazy { SplashViewModel() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_splash
    }

    override fun initView() {
        mBinding.vm = mViewModel
    }

    override fun initData() {
        verifyPermissions()
    }

    @SingleClick
    override fun onClick(v: View) {
        var intent = Intent()
        when (v?.id) {
            R.id.btn_databing -> {
                intent.setClass(this, DataBindingActivity::class.java)
                startActivity(intent)
            }
            R.id.btn_remote -> {
                intent.setClass(this, RemoteActivity::class.java)
                startActivity(intent)
            }
            R.id.btn_list -> {
                intent.setClass(this, ListActivity::class.java)
                startActivity(intent)
            }
            R.id.btn_room -> {
                intent.setClass(this, DemoRoomActivity::class.java)
                startActivity(intent)
            }
        }
    }

    private fun verifyPermissions() {
        val read_permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        val write_permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        if (read_permission != PackageManager.PERMISSION_GRANTED || write_permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    this,
                    PERMISSIONS_STREAM,
                    REQUEST_STREAM
            )
        }
    }

    /**
     * 静态变量
     */
    companion object {
        private const val REQUEST_STREAM = 1
        private val PERMISSIONS_STREAM = arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
    }
}